﻿#region Includes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenTK.Graphics.OpenGL;
#endregion

namespace GingerGL.Graphics
{
    //------------------------------------------------------------//
    /// <summary>
    /// Drawing List
    /// <para>This is a OpenGL Macro</para>
    /// </summary>
    public class DisplayList
    {
        //--------------------------------------------------------//
        #region Variables
        /// <summary>
        /// Gets OpenGL DisplayList ID
        /// </summary>
        public int DisplayListId { get; private set; }
        /// <summary>
        /// Gets TRUE if Compiling
        /// </summary>
        public bool Generating { get; private set; }

        static public DisplayListManager Manager = new DisplayListManager();
        #endregion
        //--------------------------------------------------------//
        #region Properties
        /// <summary>
        /// Gets TRUE if list has been initailized
        /// </summary>
        public bool Valid { get { return DisplayListId != -1; } }     //? is there a ObjectID?
        #endregion
        //--------------------------------------------------------//
        #region Methods
        /// <summary>
        /// Reset the display list to original state
        /// </summary>
        public void Invalidate()
        {
            if (Generating) End();                              //stop is creating
            if (DisplayListId != -1) 
                GL.DeleteLists(DisplayListId, 1);               //delete list
            DisplayListId = -1;                                 //reset data
        }
        /// <summary>
        /// Start Recording List
        /// </summary>
        public void Start() { Start(false); }                   //start recording
        /// <summary>
        /// Start Recording, Optional Execution Post Compile
        /// </summary>
        /// <param name="execute">TRUE to execute after compile</param>
        public void Start(bool execute)
        {
            Invalidate();                                       //reset to original state
            DisplayListId = GL.GenLists(1);                     //create list id
            if (execute)                                        //compile and execute
                GL.NewList(DisplayListId, ListMode.CompileAndExecute);
            else                                                //compile only
                GL.NewList(DisplayListId, ListMode.Compile);
            Generating = true;                                  //flag that object is creating
        }
        /// <summary>
        /// End Recording
        /// </summary>
        public void End()
        {
            if (Generating) GL.EndList();
            Generating = false;
        }
        /// <summary>
        /// Execute the Display List
        /// </summary>
        /// <returns>TRUE if successfull</returns>
        public bool Call()
        {
            if (Generating || !Valid) return false;
            GL.CallList(DisplayListId);
            return true;
        }
        #endregion
        //--------------------------------------------------------//
    }
    //------------------------------------------------------------//
}